const app = getApp();
const Controller = require('../../controllers/controllers').controller;
Page({
  data: {
    title:'达人搜索结果',
    searchlists:[],
    page: 1,
    pageSize: 10,
    type: 1,
    isEnd: false,
    userInfo:'',
    inputValue:'',
    isloading:false,
  },
  onLoad: function (options) {
    this.setData({
        keyword: options.search || ''
    });
    if (wx.getStorageSync('logintoken')) {
      this.setData({
        userInfo:wx.getStorageSync('userInfo')
      })
    }
  },
  onReady() {
      this.Dialog = this.selectComponent('#Dialog');
  },
  bindInput(e){
    var value=e.detail.value || ''
    if(value){
      this.setData({
        inputValue: value
      })
    }
  },
  search() {
    Controller.search({
      page: this.data.page,
      pageSize: this.data.pageSize,
      keyword: this.data.inputValue,
    }).then(res => {
      if(res.data.baseData && res.data.baseData.list && res.data.baseData.list.length>0){
        for(let item of res.data.baseData.list){
          item.followed=0
        }
        this.setData({
          searchlists: this.data.searchlists.concat(res.data.baseData.list)
        })
        if(res.data.baseData.lastPage){
          this.setData({
              isEnd: true
          })
        }else{
          this.setData({
              isEnd: false
          })
        }
      }
      this.setData({
        isloading:true
      })
    }).catch(res=>{
      this.setData({
        isloading:true
      })
    })
  },
  searchSubmit(){
    this.setData({
      searchlists:[],
      page:1,
      pageSize:10,
      isEnd:false
    })
    this.search()
  },
  scrollend() {
      if (this.data.isEnd) {
        return;
      } else {
        this.setData({
            page: this.data.page + 1,
        });
        this.rankPage()
      }
  },
  follow(e){
    var index=e.currentTarget.dataset.index;
    var id=e.currentTarget.dataset.id;
    Controller.follow({
      followedUserId:id
    }).then(res => {
      this.Dialog.ShowDialog({
        type: 'Message',
        title: '关注成功！'
      });
      this.data.searchlists[index].followed=1
      this.setData({
        searchlists:this.data.searchlists
      })
    }).catch(res =>{
      if(res.code=-1){
        this.data.searchlists[index].followed=1
        this.setData({
          searchlists:this.data.searchlists
        })
      }
    })
  },
  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·达人搜索页',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·达人搜索页',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})